<template>
  <div class="user-layout">
    <el-container>
      <el-header>
        <div class="header-container">
          <div class="logo">拼刀刀商城</div>
          <div class="user-info">
            <span class="welcome-text">欢迎，{{ userInfo.nickname || userInfo.username || '用户' }}</span>
            <el-button type="text" class="logout-button" @click="confirmLogout">退出登录</el-button>
          </div>
        </div>
      </el-header>

      <el-container>
        <el-aside width="200px">
          <el-menu
            :default-active="activeMenu"
            class="el-menu-vertical"
            router
          >

            <el-menu-item index="/user/products">
              <el-icon><shopping-bag /></el-icon>
              <span>商品展示</span>
            </el-menu-item>
            <el-menu-item index="/user/orders">
              <el-icon><tickets /></el-icon>
              <span>订单详情</span>
            </el-menu-item>
            <el-menu-item index="/user/address">
              <el-icon><location /></el-icon>
              <span>地址管理</span>
            </el-menu-item>
            <el-menu-item index="/user/profile">
              <el-icon><user /></el-icon>
              <span>个人中心</span>
            </el-menu-item>
          </el-menu>
        </el-aside>

        <el-main>
          <router-view />
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script lang="ts" setup>
import { computed, onMounted, watch } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useUserStore } from '@/store/user'
import { ElMessage, ElMessageBox } from 'element-plus'
import { ArrowDown, ShoppingBag, Tickets, Location, User, Money } from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()

// 计算当前激活的菜单
const activeMenu = computed(() => {
  return route.path
})

// 用户信息
const userInfo = computed(() => {
  return userStore.userInfo
})

// 获取用户信息
onMounted(async () => {
  if (Object.keys(userInfo.value).length === 0) {
    await userStore.getUserInfo()
  }
})

// 监听路由变化，重置滚动位置
watch(() => route.path, () => {
  // 确保主内容区域滚动到顶部
  document.querySelector('.el-main')?.scrollTo({
    top: 0,
    behavior: 'instant'
  })
})

// 确认退出登录
const confirmLogout = () => {
  ElMessageBox.confirm('确定要退出登录吗?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    userStore.logout()
  }).catch(() => {})
}
</script>

<style scoped>
.user-layout {
  height: 100vh;
  position: relative;
}

.el-header {
  background-color: #409EFF;
  color: #fff;
  line-height: 60px;
  padding: 0 20px;
  position: fixed; /* 固定头部 */
  width: calc(100% - 200px); /* 减去侧边栏宽度 */
  top: 0;
  left: 200px; /* 侧边栏宽度 */
  right: 0;
  z-index: 10;
  height: 60px;
}

.header-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.logo {
  font-size: 20px;
  font-weight: bold;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 15px;
}

.welcome-text {
  color: #fff;
  font-size: 14px;
}

.logout-button {
  color: #fff;
  font-size: 14px;
  padding: 0;
  margin-left: 10px;
}

.logout-button:hover {
  color: #f2f2f2;
  text-decoration: underline;
}

.el-aside {
  background-color: #f5f7fa;
  position: fixed; /* 固定侧边栏 */
  left: 0;
  top: 0;
  bottom: 0;
  width: 200px !important; /* 确保宽度固定 */
  z-index: 20;
  overflow-y: auto; /* 如果菜单项过多，允许滚动 */
}

.el-menu-vertical {
  height: 100%;
}

.el-main {
  background-color: #f5f7fa;
  padding: 20px;
  margin-left: 200px; /* 为侧边栏留出空间 */
  margin-top: 60px; /* 为头部留出空间 */
  min-height: calc(100vh - 60px); /* 减去头部高度 */
  overflow-y: auto; /* 允许内容区域滚动 */
  box-sizing: border-box;
  position: relative; /* 确保定位正确 */
}
</style>
